package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class ScheduledTasks {

    @Autowired
     private LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "0 41 12 * * ?")
    public void updateLeaseAgreementStatus(){

        LambdaUpdateWrapper<LeaseAgreement> wrapper=new LambdaUpdateWrapper<>();
        Date now=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String leaseEndDate = sdf.format(now);
        wrapper.eq(LeaseAgreement::getLeaseEndDate,leaseEndDate)
                .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING)
                .set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        leaseAgreementService.update(wrapper);
    }
}
